package com.furtherstar.auth.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.furtherstar.auth.controller.dto.AppResourceTree;
import com.furtherstar.auth.dao.model.AppResource;
import com.furtherstar.auth.service.AppResourceService;
import com.yummy.convention.exception.AssertUtil;
import com.yummy.convention.http.Response;

@RestController
@RequestMapping("/resource")
public class AppResourceController {
	
	@Autowired
	private AppResourceService appResourceService;

	@PostMapping("/add")
	public Response<Long> add(@RequestBody AppResource resource){
		AssertUtil.isTrue(StringUtils.isNotBlank(resource.getAppname()),"appname不能为空");
		AssertUtil.isTrue(StringUtils.isNotBlank(resource.getName()),"名称不能为空");
		AssertUtil.notNull(resource.getType(),"资源类型不能为空");
		return Response.success(appResourceService.add(resource));
	}
	
	@PostMapping("/update")
	public Response<Long> update(@RequestBody AppResource resource){
		AssertUtil.notNull(resource.getId(),"id不能为空");
		return Response.success(appResourceService.update(resource));
	}
	
	@PostMapping("/enable")
	public Response<Long> enable(@RequestBody AppResource resource){
		AssertUtil.notNull(resource.getId(),"id不能为空");
		return Response.success(appResourceService.enable(resource.getId()));
	}
	
	@PostMapping("/disable")
	public Response<Long> disable(@RequestBody AppResource resource){
		AssertUtil.notNull(resource.getId(),"id不能为空");
		return Response.success(appResourceService.disable(resource.getId()));
	}
	
	@PostMapping("/delete")
	public Response<Boolean> delete(@RequestBody AppResource resource){
		AssertUtil.notNull(resource.getId(),"id不能为空");
		return Response.success(appResourceService.delete(resource.getId()));
	}
	
	@GetMapping("/tree")
	public Response<List<AppResourceTree>> tree(String appname){
		AssertUtil.isTrue(StringUtils.isNotBlank(appname),"appname不能为空");
		return Response.success(appResourceService.resourcesTree(appname));
	}
	
}
